/**
 * Created by YangYan on 2016/5/24.
 */
//数组的几个实用方法
(function () {
    var arr=['apple','orange','pear'],found=false;
    for(var i=0,l=arr.length;i<l;i++){
       if(arr[i]==='orange')found=true;
    };
   // console.log("found:",found);

    var arr1=['apple','orange','pear'];
   // console.log("found:",(arr.indexOf("orange"))!=-1);
   //----------------------------------------------------
    var arr2=[
        {"name":"apple","count":2},
        {"name":"orange","count":5},
        {"name":"pear","count":3},
        {"name":"orange","count":16},
    ];
    var newArr=[];
    for(var i=0,l=arr2.length;i<l;i++){
        //alert(arr2.length);
        if(arr2[i].name === "orange"){newArr.push(arr2[i]);}
    };
    //console.log(newArr);

    var newArr2=arr2.filter(function(item){
        return item.name === "orange";
    });
    //console.log(newArr2);
    //---------------------------------------------------
    var arr3=[1,2,3,4,5,6,7,8];
    for(var i=0,l=arr3.length;i<l;i++){
        //console.log(arr[i]);
    };
    arr3.forEach(function(item,index){
       //console.log(item);
    });
    //--------------------------------------------------
    var oldArr=[
        {first_name:"Colin",last_name:"Toh"},
        {first_name:"Addy",last_name:"Osmani"},
        {first_name:"Yehuda",last_name:"Katz"}
    ];
    function getNewArr(){
        var newArr3=[];
        for(var i=0,l=oldArr.length;i<l;i++){
            var item=oldArr[i];
            item.full_name=[item.first_name,item.last_name].join(" ");
            newArr3[i]=item;
        }
        return newArr3;
    };
    //console.log(getNewArr());

    function getNewArr2() {
        return oldArr.map(function(item,index){
            item.full_name=[item.first_name,item.last_name].join(" ");
            return item;
        });
    };
    //console.log(getNewArr2());
    //---------------------------------------------------------------
    var arr4=["apple","orange","apple","orange","pear","orange"];
    function getWordCnt(){
        var obj={};
        for(var i=0,l=arr4.length;i<l;i++){
            var item=arr4[i];
            //console.log(item+":"+obj[item]);
            obj[item]=(obj[item]+1) ||1;
        };
        return obj;
    };
    //console.log(getWordCnt());
    function getWordCnt1(){
        return arr.reduce(function(prev,next){
            prev[next]=(prev[next]+1) ||1;
            return prev;
        },{});
    };
    console.log(getWordCnt1());
    //-----------------------------------------------
    var arr5=["apple","orange"];
    function noPassValue(){
        return arr5.reduce(function(prev,next){
            console.log("prev:",prev);
            console.log("next:",next);
            return prev+" "+next;
        });
    };
    function passValue(){
        return arr5.reduce(function(prev,next){
            console.log("prev:",prev);
            console.log("next:",next);
            prev[next]=1;
            return prev;
        },{});
    };
    console.log("No Additional parameter:",noPassValue());
    console.log("-------------------------");
    console.log("With {} as an additional parameter:",passValue());


















    //---------------------------------------------------------















})();

